import sys
from PyQt6.QtWidgets import QApplication, QMainWindow, QWidget, QHBoxLayout, QVBoxLayout
from PyQt6.QtCore import Qt
from ui.left_panel import LeftPanel
from ui.center_panel import CenterPanel
from ui.right_panel import RightPanel
from question_bank import QuestionBank

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.question_bank = QuestionBank()  # 初始化题库
        self.setWindowTitle("智能刷题系统")
        self.setGeometry(100, 100, 1200, 800)
        
        # 创建主窗口部件
        main_widget = QWidget()
        self.setCentralWidget(main_widget)
        
        # 创建水平布局
        layout = QHBoxLayout()
        main_widget.setLayout(layout)
        
        # 创建三个面板
        self.left_panel = LeftPanel(self.question_bank)
        self.center_panel = CenterPanel(self.question_bank)
        self.right_panel = RightPanel(self.question_bank)
        
        # 设置面板比例
        layout.addWidget(self.left_panel, 1)
        layout.addWidget(self.center_panel, 2)
        layout.addWidget(self.right_panel, 1)
        
        # 连接信号
        self.connect_signals()
    
    def connect_signals(self):
        # 连接左侧面板的筛选信号到中心面板
        self.left_panel.filter_changed.connect(self.center_panel.on_filter_changed)
        
        # 连接中心面板的题目选择信号到右侧面板
        self.center_panel.question_selected.connect(self.right_panel.load_question)

        # 连接中心面板的题目删除信号
        self.center_panel.question_deleted.connect(self.on_question_deleted)
    
    def on_question_deleted(self, index: int):
        """处理题目删除事件"""
        # 如果当前正在显示被删除的题目，清空右侧面板
        if self.right_panel.current_question_index == index:
            self.right_panel.clear_chat()

def main():
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec())

if __name__ == "__main__":
    main() 